﻿using QuanLyTiemVang.BUS;
using QuanLyTiemVang.Classes;
using QuanLyTiemVang.DAO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace QuanLyTiemVang.GUI
{
    public partial class DanhSachSanPham : Window, IPhanQuyenHelper
    {
        public DanhSachSanPham()
        {
            InitializeComponent();
            this.Refresh();
        }
        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonDown(e);

            // Begin dragging the window
            this.DragMove();
        }
        private void Refresh()
        {
            txtMaSanPham.Text = SanPhamDAO.GetLastMaSanPham().ToString();
            txtTenSanPham.Text = "";
            tbSoLuongTon.Text = "";
            tbSoLuongTon.IsEnabled = true;
            btnThem.IsEnabled = true;
            btnCapNhat.IsEnabled = false;
            comboLoaiSanPham.SelectedIndex = 0;
        }
        private void btnThem_Click(object sender, RoutedEventArgs e)
        {
            if (txtTenSanPham.Text == "")
            {
                MessageBox.Show("Chưa nhập tên sản phẩm", "Lỗi", MessageBoxButton.OK, MessageBoxImage.Error);
                txtTenSanPham.Focus();
            }
            else if (tbSoLuongTon.Text == "")
            {
                MessageBox.Show("Chưa nhập số lượng tồn", "Lỗi", MessageBoxButton.OK, MessageBoxImage.Error);
                tbSoLuongTon.Focus();
            }
            else
            {
                List<LOAISANPHAM> lstLoaiSP = SanPhamDAO.GetAllLoaiSanPham();
                SANPHAM sp = new SANPHAM
                {
                    TenSanPham = txtTenSanPham.Text,
                    SoLuongTon = int.Parse(tbSoLuongTon.Text),
                    MaLoaiSanPham = lstLoaiSP[comboLoaiSanPham.SelectedIndex].MaLoaiSanPham
                };
                if (SanPhamBUS.ThemSanPham(sp))
                {
                    Window_Loaded(sender, e);

                    MessageBox.Show("Thêm thành công.", "Thông báo", MessageBoxButton.OK, MessageBoxImage.Information);
                }
                else
                    MessageBox.Show("Thêm thất bại.", "Thông báo", MessageBoxButton.OK, MessageBoxImage.Information);
            }
        }

        private void btnTimKiem_Click(object sender, RoutedEventArgs e)
        {
            TraCuuSanPham frmSanPham = new TraCuuSanPham();
            frmSanPham.Show();
        }

        private void btnCapNhat_Click(object sender, RoutedEventArgs e)
        {
            if (txtMaSanPham.Text == "")
                return;

            if (txtTenSanPham.Text == "")
            {
                MessageBox.Show("Chưa nhập tên sản phẩm", "Lỗi", MessageBoxButton.OK, MessageBoxImage.Error);
                txtTenSanPham.Focus();
                return;
            }
            else if (tbSoLuongTon.Text == "")
            {
                MessageBox.Show("Chưa nhập số lượng tồn", "Lỗi", MessageBoxButton.OK, MessageBoxImage.Error);
                tbSoLuongTon.Focus();
                return;
            }

            List<LOAISANPHAM> lstLoaiKhach = SanPhamDAO.GetAllLoaiSanPham();
            SANPHAM sp = new SANPHAM
            {
                MaSanPham = int.Parse(txtMaSanPham.Text),
                TenSanPham = txtTenSanPham.Text,
                SoLuongTon = int.Parse(tbSoLuongTon.Text),
                MaLoaiSanPham = lstLoaiKhach[comboLoaiSanPham.SelectedIndex].MaLoaiSanPham
            };
            if (SanPhamBUS.CapNhatSanPham(sp))
            {
                Window_Loaded(sender, e);
                Refresh();
                MessageBox.Show("Cập nhật thành công.", "Thông báo", MessageBoxButton.OK, MessageBoxImage.Information);
            }
            else
                MessageBox.Show("Cập nhật thất bại.", "Thông báo", MessageBoxButton.OK, MessageBoxImage.Information);
        }

        private void btnThemMoi_Click(object sender, RoutedEventArgs e)
        {
            Refresh();
        }

        private void btnXoa_Click(object sender, RoutedEventArgs e)
        {
            List<SANPHAM> lstSanPham = SanPhamDAO.GetAllSanPham();
            bool isSuccessful = false;

            foreach (SANPHAM item in lstSanPham)
            {
                if (item.IsChecked)
                {
                    isSuccessful = true;
                }
            }

            if (isSuccessful == false)
                return;

            MessageBoxResult result = MessageBox.Show("Bạn có chắc chắn muốn xóa các trường đã chọn?", "Xác nhận xóa", MessageBoxButton.YesNo, MessageBoxImage.Question);
            if (result == MessageBoxResult.No)
            {
                return;
            }

            foreach (SANPHAM item in lstSanPham)
            {
                if (item.IsChecked)
                {
                    isSuccessful = true;
                    if (SanPhamBUS.XoaSanPham(item))
                    {

                        //MessageBox.Show("Xóa thành công");
                    }
                    else
                    {
                        MessageBox.Show("Xoá thất bại.", "Thông báo", MessageBoxButton.OK, MessageBoxImage.Information);
                        return;
                    }
                }
            }
            Window_Loaded(sender, e);
            Refresh();
            MessageBox.Show("Xóa thành công.", "Thông báo", MessageBoxButton.OK, MessageBoxImage.Information);
        }

        private void btnThoat_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            List<SANPHAM> dt = SanPhamDAO.GetAllSanPham();
            this.Refresh();
            dgDanhSachSanPham.ItemsSource = null;
            dgDanhSachSanPham.ItemsSource = dt;
            comboLoaiSanPham.ItemsSource = SanPhamDAO.GetAllTenLoaiSanPham();
        }

        private void dgDanhSachSanPham_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            int index = dgDanhSachSanPham.SelectedIndex;
            List<SANPHAM> lstKhach = SanPhamDAO.GetAllSanPham();
            List<LOAISANPHAM> lstLoaiSP = SanPhamDAO.GetAllLoaiSanPham();
            if (index < 0 || index >= lstKhach.Count)
                return;
            //lstKhach[index].IsChecked = !lstKhach[index].IsChecked;

            //dgDanhSachSanPham.ItemsSource = null;
            dgDanhSachSanPham.ItemsSource = lstKhach;
            
            //if (lstKhach[index].IsChecked)
            {
                txtMaSanPham.Text = lstKhach[index].MaSanPham.ToString();
                txtTenSanPham.Text = lstKhach[index].TenSanPham;
                tbSoLuongTon.Text = lstKhach[index].SoLuongTon.ToString();
                comboLoaiSanPham.SelectedIndex = lstLoaiSP.IndexOf(lstKhach[index].LOAISANPHAM);
            }

            btnCapNhat.IsEnabled = true;
            btnThem.IsEnabled = false;
        }

        private void dgDanhSachSanPham_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int index = dgDanhSachSanPham.SelectedIndex;
            List<SANPHAM> lstKhach = SanPhamDAO.GetAllSanPham();
            List<LOAISANPHAM> lstLoaiSP = SanPhamDAO.GetAllLoaiSanPham();
            if (index < 0 || index >= lstKhach.Count)
                return;
            //lstKhach[index].IsChecked = !lstKhach[index].IsChecked;

            tbSoLuongTon.IsEnabled = false;
            //dgDanhSachSanPham.ItemsSource = null;
            dgDanhSachSanPham.ItemsSource = lstKhach;

            //if (lstKhach[index].IsChecked)
            {
                txtMaSanPham.Text = lstKhach[index].MaSanPham.ToString();
                txtTenSanPham.Text = lstKhach[index].TenSanPham;
                tbSoLuongTon.Text = lstKhach[index].SoLuongTon.ToString();
                comboLoaiSanPham.SelectedIndex = lstLoaiSP.IndexOf(lstKhach[index].LOAISANPHAM);
            }

            btnCapNhat.IsEnabled = true;
            btnThem.IsEnabled = false;
        }

        void IPhanQuyenHelper.SetAdminFunction()
        {
            btnThem.IsEnabled = true;
            btnCapNhat.IsEnabled = true;
            btnXoa.IsEnabled = true;
        }

        void IPhanQuyenHelper.SetStaffFunction()
        {
            btnThem.IsEnabled = true;
            btnCapNhat.IsEnabled = true;
            btnXoa.IsEnabled = false;
        }

        void IPhanQuyenHelper.SetCustomerFunction()
        {
            btnThem.IsEnabled = false;
            btnCapNhat.IsEnabled = false;
            btnXoa.IsEnabled = false;
        }

        void IPhanQuyenHelper.SetNonFunction()
        {
            this.Close();
        }

        private void tbSoLuongTon_PreviewTextInput_1(object sender, TextCompositionEventArgs e)
        {
            int result;
            if (!(int.TryParse(e.Text, out result)))
            {
                e.Handled = true;
            }
        }
    }
}
